package kr.saeildamil.androidstudy_second.fragment.common;

import kr.saeildamil.andrordstudy.second.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class CounterFragment02 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.ch20_counterfragment, container, false);
		return root;
	}//end of method onCreateView
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Button btnIncrease = (Button) getView().findViewById(R.id.counterfragment_btn);
		final TextView textCounter = (TextView) getView().findViewById(R.id.counterfragment_txtcounter);
		
		if (savedInstanceState !=null){
			textCounter.setText(
					Integer.toString(
							savedInstanceState.getInt("counter")
					)
			);
		}
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int count = Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toString(count+1));
			}
		});
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		TextView textCounter = (TextView) getView().findViewById(R.id.counterfragment_txtcounter);
		int count = Integer.parseInt(textCounter.getText().toString());
		outState.putInt("counter", count);
	}
}








